package com.cloud.bc.service.impl;

import cn.hutool.core.util.NumberUtil;
import com.cloud.bc.model.entity.BcFinanceRecords;
import com.cloud.bc.model.entity.BcFinances;
import com.cloud.bc.mapper.BcFinancesMapper;
import com.cloud.bc.service.BcFinanceRecordsService;
import com.cloud.bc.service.BcFinancesService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloud.common.model.enums.CurrencyTypeEnum;
import lombok.AllArgsConstructor;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;

/**
 * 财务
 *
 * @author douhaichao code generator
 * @date 2021-01-13 11:19:15
 */
@Service
@AllArgsConstructor
public class BcFinancesServiceImpl extends ServiceImpl<BcFinancesMapper, BcFinances> implements BcFinancesService {

    private final BcFinanceRecordsService bcFinanceRecordsService;

    /**
     * 根据对象属性查找满足条件的第一条数据     *      * @return
     */
    @Override
    public BcFinances selectOne(BcFinances req) {
        QueryWrapper queryWrapper = new QueryWrapper<BcFinances>();
        queryWrapper.setEntity(req);
        return baseMapper.selectOne(queryWrapper);
    }

    /**
     * 根据条件查询所有
     */
    @Override
    public List<BcFinances> selectAll(BcFinances req) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.setEntity(req);
        return baseMapper.selectList(queryWrapper);
    }

    /**
     * 根据对象查询所有分页数据(精确查询)     *      * @return
     */
    @Override
    public IPage<BcFinances> selectPage(Page page, BcFinances req) {
        IPage<BcFinances> list = baseMapper.selectPage(
                page,
                new QueryWrapper<BcFinances>().setEntity(req));
        return list;
    }

    /**
     * 根据对象查询所有分页数据(模糊查询)     *      * @return
     */
    @Override
    public IPage<BcFinances> selectPageByLike(Page page, BcFinances req) {
        QueryWrapper queryWrapper = new QueryWrapper();

        if (null != req.getUserId()) {
            queryWrapper.eq("user_id", req.getUserId());
        }
        IPage<BcFinances> list = baseMapper.selectPage(page, queryWrapper);
        return list;
    }

    /**
     * 根据条件统计数据量     *      * @return
     */
    @Override
    public int count(BcFinances req) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.setEntity(req);
        return baseMapper.selectCount(queryWrapper);
    }

    @Override
    public boolean add(BcFinances bcFinances) {
        return baseMapper.add(bcFinances);
    }

    //减少余额
    @Override
    public boolean sub(BcFinances bcFinances) {
        return baseMapper.sub(bcFinances);
    }

    @Transactional
    @Override
    public boolean exchange(BcFinances bcFinances) {
        //现金与喜糖比例为1：100
        bcFinances.setMoney(NumberUtil.div(bcFinances.getCandyPrice(), 100));
        //扣喜糖，增加现金
        boolean deduction = baseMapper.deductionCandy(bcFinances);
        if (!deduction) {
            //您没有足够的喜糖，不能兑换
            return false;
        }
//记录扣减喜糖流水
        BigDecimal candyPrice = bcFinances.getCandyPrice();
        BcFinanceRecords bcFinanceRecords = new BcFinanceRecords();
        bcFinanceRecords.setUserId(bcFinances.getUserId());
        bcFinanceRecords.setCurrencyType(CurrencyTypeEnum.CANDY.getCode());
        bcFinanceRecords.setType(0);
        bcFinanceRecords.setPid(0);
        bcFinanceRecords.setIncome(candyPrice);
        bcFinanceRecords.setRemark("喜糖兑换:" + candyPrice);
        bcFinanceRecordsService.recodeAdd(bcFinanceRecords);
        //增加现金流水
        BigDecimal moneyPrice = NumberUtil.div(candyPrice, 100);
        BcFinanceRecords bcFinanceRecordsMoney = new BcFinanceRecords();
        bcFinanceRecordsMoney.setUserId(bcFinances.getUserId());
        bcFinanceRecordsMoney.setCurrencyType(CurrencyTypeEnum.CASH.getCode());
        bcFinanceRecordsMoney.setType(0);
        bcFinanceRecordsMoney.setPid(0);
        bcFinanceRecordsMoney.setIncome(moneyPrice);
        bcFinanceRecordsMoney.setRemark("现金兑换:" + NumberUtil.round(moneyPrice, 2));
        bcFinanceRecordsService.recodeAdd(bcFinanceRecordsMoney);
        return true;
    }
}
